package com.microstation.service.impl;

import java.util.List;

import com.microstation.dao.AdminInfoDao;
import com.microstation.enums.ResultEnum;
import com.microstation.exception.RecordHaveException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.microstation.service.RoleInfoService;
import com.microstation.dao.RoleInfoDao;
import com.microstation.common.Assist;
import com.microstation.common.Result;
import com.microstation.entity.po.RoleInfo;
import org.springframework.transaction.annotation.Transactional;

/**
 * RoleInfo的服务接口的实现类
 *
 * @author
 */
@Service
public class RoleInfoServiceImpl implements RoleInfoService {
    private final Logger LOG = LoggerFactory.getLogger(this.getClass());

    @Autowired
    private RoleInfoDao roleInfoDao;
    @Autowired
    private AdminInfoDao adminInfoDao;

    @Override
    public Result find(Assist assist) {
        //TODO这里可以做通过Assist做添加查询
        long results = roleInfoDao.getRoleInfoRowCount(assist);
        List<RoleInfo> result = roleInfoDao.selectRoleInfo(assist);
        return new Result(results, result);
    }

    @Override
    public RoleInfo findOne(Long id) {
        if (id == null) {
            return null;
        }
        RoleInfo result = roleInfoDao.selectRoleInfoById(id);
        return result;
    }

    @Override
    public Integer saveNotNull(RoleInfo value) {
        if (value == null) {
            return null;
        }
        int result = roleInfoDao.insertNotNullRoleInfo(value);
        return result;
    }

    @Override
    public Integer updateNotNullById(RoleInfo value) {
        if (value == null) {
            return null;
        }
        int result = roleInfoDao.updateNotNullRoleInfoById(value);
        return result;
    }

    @Override
    public Integer deleteById(Long id) {
        if (id == null) {
            return null;
        }
        int result = roleInfoDao.deleteRoleInfoById(id);
        return result;
    }

    @Override
    @Transactional
    public Result saveRole(RoleInfo roleInfo) {
        try {
            int result = roleInfoDao.insertNotNullRoleInfo(roleInfo);
            if (result != 0) {
                return new Result(false, ResultEnum.SUCCESS, null);
            }
            throw new RecordHaveException("已存在相同角色名");
            //return new ReturnError(true, ReturnInfoEnum.FAIL.getType(), "已存在相同角色名", null);
        } catch (RecordHaveException e) {
            throw e;
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    @Transactional
    public Result updateRoleInfo(RoleInfo roleInfo) {
        try {
            int result = roleInfoDao.updateNotNullRoleInfoById(roleInfo);
            if (result != 0) {
                return new Result(false, ResultEnum.SUCCESS, null);
            }
            return new Result(true, ResultEnum.FAIL.getType(), "已存在相同角色名", null);
        } catch (Exception e) {
            e.printStackTrace();
            throw new RuntimeException(e);
        }
    }

    @Override
    @Transactional
    public Result removeRole(String ids) {
        String[] idarr = ids.split(",");
        try {
            for (String id : idarr) {
                Long roleId = Long.parseLong(id);
                if (roleId.longValue() == 1) {
                    continue;
                }
                int i = roleInfoDao.deleteRoleInfoById(roleId);
                if (i != 0) {
                    Assist assist = new Assist();
                    assist.andEq("role_id", roleId);
                    adminInfoDao.deleteAdminInfoByAssist(assist);
                }

            }
            return new Result(false, ResultEnum.SUCCESS.getType(), null);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }
}